package com.uam.web.common.utils;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.LineSegment;
import org.locationtech.jts.math.Vector2D;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LineSegmentUtils {

    // 创建多个点组成的线段
    public static List<LineSegment> createLineSegments(List<Coordinate> points) {
        List<LineSegment> lines = new ArrayList<>();
        for (int i = 0; i < points.size() - 1; i++) {
            lines.add(new LineSegment(points.get(i), points.get(i + 1)));
        }
        return lines;
    }

    // 创建与给定线段平行的另一条线段列表
    public static List<LineSegment> createParallelLines(LineSegment line, Vector2D lineVector, double distance) {
        List<LineSegment> parallelLines = new ArrayList<>();

        // 获取单位法向量
        Vector2D normalVector = lineVector.rotateByQuarterCircle(1).normalize();

        // 计算平行线段的端点
        Coordinate p0 = new Coordinate(line.p0.x + normalVector.getX() * distance, line.p0.y + normalVector.getY() * distance);
        Coordinate p1 = new Coordinate(line.p1.x + normalVector.getX() * distance, line.p1.y + normalVector.getY() * distance);
        parallelLines.add(new LineSegment(p0, p1));

        return parallelLines;
    }

    /**
     * 获取平行线段
     * @param points 点
     * @param distance  小数点后面5 为米
     * @return
     */
    public static List<LineSegment>  parallelLines(List<Coordinate> points,double distance){
        // 创建线段
        List<LineSegment> originalLines = createLineSegments(points);

        // 计算每个线段的向量表示
        List<Vector2D> lineVectors = new ArrayList<>();
        for (LineSegment line : originalLines) {
            lineVectors.add(new Vector2D(line.p0, line.p1));
        }

        // 计算平行线段列表
        List<LineSegment> parallelLines = new ArrayList<>();
        for (int i = 0; i < originalLines.size(); i++) {
            LineSegment originalLine = originalLines.get(i);
            Vector2D lineVector = lineVectors.get(i);
            // 平行距离为 2
            parallelLines.addAll(createParallelLines(originalLine, lineVector, distance));
        }

        return parallelLines;
    }

    public static List<Coordinate> parallelLinesPoints(List<Coordinate> points,double distance){
        List<LineSegment>  parallelLines=parallelLines(points,distance);

        List<Coordinate> returnListInfo=new ArrayList<>(points.size());
        if(parallelLines.size() == 2){
            returnListInfo.add(parallelLines.get(0).p0);
            returnListInfo.add(parallelLines.get(0).p1);
            returnListInfo.add(parallelLines.get(1).p1);
            return returnListInfo;
        }

        returnListInfo.add(parallelLines.get(0).p0);
        int row=parallelLines.size();
        for(int i=1;i<row;i++){
            returnListInfo.add(parallelLines.get(i).p0);
        }
        returnListInfo.add(parallelLines.get(parallelLines.size()-1).p1);
        return returnListInfo;
    }

    public static void main(String[] args) {
        // 创建多个点组成的线段
        //112.97360514514861,28.25000115239046;112.97176586714133,28.250675647155795;112.96992637413378,28.250001582868546
        List<Coordinate> points = new ArrayList<>();
        points.add(new Coordinate(112.97360514514861, 28.25000115239046));
        points.add(new Coordinate(112.97176586714133, 28.250675647155795));
        points.add(new Coordinate(112.97175586714133, 28.250665647155795));
        points.add(new Coordinate(112.96992637413378, 28.250001582868546));




        List<Coordinate> coordinates=  parallelLinesPoints(points,0.000031);
        System.out.println(coordinates.size());
    }
}
